package com.ityu.cloudclientone.feign;


import org.springframework.cloud.client.circuitbreaker.NoFallbackAvailableException;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * author: yu.jl
 * e-mail: bbfx89625@gmail.com
 * time  : 2021/6/25
 * desc  :
 */
@FeignClient(value = "cloudserver",
        configuration = FeignConfig.class,
        fallbackFactory = ApiClient.ApiClientFallbackFactory.class)
@Component
public interface ApiClient {
    @GetMapping(value = "/testError")
    String testError();

    @GetMapping(value = "/testError2")
    String testError2();

    @GetMapping(value = "/testServerTwo/{name}/{content}")
    String testServerTwo(@PathVariable String name, @PathVariable String content);

    @Component
    class ApiClientFallbackFactory implements FallbackFactory<FallbackWithFactory> {

        @Override
        public FallbackWithFactory create(Throwable cause) {
            System.out.println("create");
            return new FallbackWithFactory();
        }

    }

    class FallbackWithFactory implements ApiClient {

        @Override
        public String testError() {
            System.out.println("testError");
            throw new NoFallbackAvailableException("熔断器起作用了-testServerOne", new RuntimeException());
        }

        @Override
        public String testError2() {
            System.out.println("testError2");
            return "熔断器起作用了-testError2";
        }

        @Override
        public String testServerTwo(String name, String content) {
            return "正常调用" + name + content;
        }

    }

}


